package com.cnap.multiclouldmgmt.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
/**
 * @author dyq
 * 工具类
 */
public class FileUtils {

    /**
     * 文件内容转换成字符串
     * @param file
     * @return
     * @throws IOException
     */
    public static String getFileContent(MultipartFile file) throws IOException {

        StringBuilder content = new StringBuilder();

        InputStream is = file.getInputStream();
        InputStreamReader isReader = new InputStreamReader(is, StandardCharsets.UTF_8);
        BufferedReader br = new BufferedReader(isReader);
        //循环逐行读取
        while (br.ready()) {
            content.append(br.readLine()).append("\n");
        }
        //关闭流，讲究
        br.close();

        return content.toString();
    }
}
